<?php

require('barDB.php');

function SRMToRGB( $srm )
{    
    $srmTable = array(
        0xF3F993,
        0xF3F993,
        0xF5F75C,
        0xF6F513,
        0xEAE615,
        0xE0D01B,
        0xD5BC26,
        0xCDAA37,
        0xC1963C,
        0xBE8C3A,
        0xBE823A,
        0xC17A37,
        0xBF7138,
        0xBC6733,
        0xB26033,
        0xA85839,
        0x985336,
        0x8D4C32,
        0x7C452D,
        0x6B3A1E,
        0x5D341A,
        0x4E2A0C,
        0x4A2727,
        0x361F1B,
        0x261716,
        0x231716,
        0x19100F,
        0x16100F,
        0x120D0C,
        0x100B0A,
        0x050B0A,
    );
    
    $maxIndex = count( $srmTable ) - 1;
    
    $tableIndexLow = min( $maxIndex, max( 0, floor( $srm ) ), floor( $srm ) );
    $tableIndexHigh = min( $maxIndex, max( 0, ceil( $srm ) ), ceil( $srm ) );
    
    $hexHigh = $srmTable[$tableIndexHigh];
    $hexLow = $srmTable[$tableIndexLow];
   
    $rgbHigh = array( $hexHigh >> 16, ($hexHigh >> 8) & 0xff, $hexHigh & 0xff );
    $rgbLow = array( $hexLow >> 16, ($hexLow >> 8) & 0xff, $hexLow & 0xff );
    
    $lerpAmount = 1.0 - ( ceil( $srm ) - $srm );
    
    $rgb = array( round( $rgbLow[0] + ($rgbHigh[0] - $rgbLow[0]) * $lerpAmount ),
        round( $rgbLow[1] + ($rgbHigh[1] - $rgbLow[1]) * $lerpAmount ),
        round( $rgbLow[2] + ($rgbHigh[2] - $rgbLow[2]) * $lerpAmount ) );
   
    
    return $rgb;
}

//
//
//
function myvar_dump( $obj, $name )
{
    $type = gettype($obj);
    print "Dumping $name -  $type <br/>\n";
    var_dump( $obj );
    print "END<br/>\n";
}

//
//
//
function GetRecentTempertureData()
{
    $NUM_HOURS_OF_DATA = 48;

    $NUM_LABELS = 5;
    $SECONDS_PER_HOUR = 3600;
    $MLLI_SECONDS_PER_HOUR = $SECONDS_PER_HOUR * 1000;
    
    $db = new BeerDB();
    
    $now = time();
    $nowHours = $now - ( $now % $SECONDS_PER_HOUR );
    $hoursAgo = strtotime( "-$NUM_HOURS_OF_DATA hours", $nowHours );
    
    $rows = $db->GetTemperatureReadingsSince( $hoursAgo );

    $dateTemp = [];

    // extract time and temperature data form the query

    while( $row = $rows->fetchArray( SQLITE3_ASSOC ) )
    {
        array_push( $dateTemp, [ $row['temperatureepochdate'] * 1000,  $row['temperature'] ] );
    }
    
    if( count( $dateTemp ) == 0 )
    {
        $last = $hoursAgo * 1000;
    }
    else
    {
        $last = end( $dateTemp )[0];
    }

    
    $nowMilli = $now * 1000;
    while( $last <= $nowMilli ) 
    {
        array_push( $dateTemp, [ $last,  null ] );        
        $last += $MLLI_SECONDS_PER_HOUR;
    }

    $db->close();

    return $dateTemp;
}

//
//
//
function GetTapPercentRemaining( $tapId )
{
    $result = 0;
    
    $db = new BeerDB();
    
    $rows = $db->GetRemainingPercentage( $tapId );
    
    while( $row = $rows->fetchArray( SQLITE3_ASSOC ) )
    {
        $result = $row['percentremaining'];
    }
    
    $db->close();
    
    return $result;
}

//
//
//
function RecentPourData( $tapid )
{
    $db = new BeerDB();
    $tapData = $db->GetTapPourData( $tapid );
    
    $pourData = [];
    
    while( $row = $tapData->fetchArray( SQLITE3_ASSOC ) )
    {
        $row['pourepochdate'] *= 1000;
        array_push( $pourData, $row );
    }
    
    $db->close();
    
    return $pourData;
}

//
//
//
function RenderTemperatureChart()
{
    $tempertureData = RecentTempertureData();
  
    $series1 = new HighRollerSeriesData();
    $series1->addName('Keg Temperature')->addData($tempertureData);
    //$series1->pointStart = $tempertureData[0][0];
    //$series1->pointInterval = 24 * 3600 * 1000; // one day

    $linechart = new HighRollerSplineChart();
    $linechart->chart->renderTo = 'temperaturelinechart';
    //$linechart->chart->width = 500;
    $linechart->title->text = 'Keg Temperture';

    $linechart->yAxis = new HighRollerYAxis();
    $linechart->yAxis->title->text = 'Temperture';

    $linechart->xAxis = new HighRollerXAxis();
    $linechart->xAxis->type= 'datetime';
    //$linechart->xAxis->startOnTick = true;

    unset($linechart->xAxis->startOnTick);
    unset($linechart->xAxis->categories);
    unset($linechart->xAxis->tickInterval);
    unset($linechart->xAxis->tickPixelInterval);
    unset($linechart->xAxis->formatter);

    $linechart->xAxis->dateTimeLabelFormats->year = '%e. %b %Y';
    $linechart->xAxis->dateTimeLabelFormats->month = '%e. %b';
    $linechart->xAxis->dateTimeLabelFormats->minute = '%e %l:%M%p';
    $linechart->xAxis->dateTimeLabelFormats->hour = '%e %b %l%p';
    $linechart->xAxis->dateTimeLabelFormats->day = '%e %b';

    $linechart->addSeries($series1);

    $linechart->renderChartOptionsObject('temperaturelinechart');
    
?>
    <script type="text/javascript">
        <?php echo $linechart->renderChart();?>
    </script>
<?php
}

//
//
//
function GetTapSummary( $tapid )
{
    $db = new BeerDB();
    
    $tapData = [];
    
    $data = $db->GetTapBeerData( $tapid );
    
    $row = $data->fetchArray( SQLITE3_ASSOC );
    
    $db->close();
    
    return $row;
}


?>